package com.longrise.android.byjk.plugins.vip.myintegral;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.IntegralTaskEvent;
import com.longrise.android.byjk.event.MineRefreshEvent;
import com.longrise.android.byjk.event.MyIntegralEvent;
import com.longrise.android.byjk.plugins.vip.integralexchange.IntegralTaskUtil;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.android.byjk.utils.DialogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2017/4/27.
 * 我的积分 相应任务做完了会弹出弹窗
 */

public class MyIntegralDialogUtil {


    /**
     * 详情见下面方法
     */
    public static void showIntegralDailog(Activity activity, String scores, String msg) {
//        showIntegralDailog(activity, scores, msg, false);
    }

    /**
     * @param activity         当前操作的activity
     * @param scores           任务完成获取的分数
     * @param msg              后台给的meg
     * @param isFromMyIntegral 当前这个页面是否是从积分任务跳转过来的,是则:true  否则:false
     *                         注意: 20170808 取消isFromMyIntegral这个字段
     */
    public static void showIntegralDailog(Activity activity, String scores, String msg, boolean isFromMyIntegral) {
        if (ActivityUtil.containThisActivity(activity)) {
            View dialogView = View.inflate(activity, R.layout.dialog_integra_getsucc, null);
            TextView tv_cancel = (TextView) dialogView.findViewById(R.id.dialog_integra_get_succ_wzdl_tv);
            TextView tv_confirm = (TextView) dialogView.findViewById(R.id.dialog_integra_get_succ_jxzrw_tv);
            TextView tv_scores = (TextView) dialogView.findViewById(R.id.dialog_integra_get_succ_scores);
            TextView tv_msg = (TextView) dialogView.findViewById(R.id.dialog_integra_get_succ_msg);
            tv_scores.setText("+" + scores);
            tv_msg.setText(msg);
            iniEvent(activity, tv_cancel, tv_confirm, isFromMyIntegral);

            //弹窗表示赚取了积分 那么积分显示就会变化
            MineRefreshEvent event = new MineRefreshEvent();
            event.setBBye(true);
            event.setName(true);//有可能更改昵称
            EventBus.getDefault().post(event); //刷新我的页面
            EventBus.getDefault().post(new MyIntegralEvent(true)); //刷新我的积分页面

            Dialog mDialog = DialogUtil.getInstance().creatAlertDialog(activity, dialogView, 270, 355);
            DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    } else {
                        return false;
                    }
                }
            };
            mDialog.setOnKeyListener(keylistener);
            mDialog.show();
        }
    }

    private static void iniEvent(final Activity activity, TextView tv_cancel, TextView tv_confirm,
                                 final boolean isFromMyIntegral) {
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntegralTaskEvent event = new IntegralTaskEvent();
                event.setRefresh(true);
                EventBus.getDefault().post(event);//刷新积分任务

                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                IntegralTaskEvent event = new IntegralTaskEvent();
                event.setRefresh(true);
                EventBus.getDefault().post(event);//刷新积分任务
                IntegralTaskUtil.toIntegralTask(activity);

            }
        });
    }
}
